package com.scsj.supermarket.view.fragment.shopdetailmodel;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.ShopPointsAdapter;
import com.scsj.supermarket.bean.GoodsSortBean;
import com.scsj.supermarket.customview.GridSpacingItemDecoration;
import com.scsj.supermarket.interfaces.GoodsType;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import static com.scsj.supermarket.adapter.GoodsSortAdapter.GRID;
import static com.scsj.supermarket.adapter.GoodsSortAdapter.LINEAR;


/**
 * 店铺详情-积分换购
 */
public class ShopPointsFragment extends BaseFragment implements View.OnClickListener{

    private LinearLayout array_type_ll;
    private ImageView array_type_iv;

    public int arrayType = GRID;//排列方式 0 网格 1 列表
    private RecyclerView recycler_view;
    private GridSpacingItemDecoration decor;
    private ShopPointsAdapter shopPointsAdapter;
    private List<GoodsSortBean> mDatas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_points, null);
        initView(view, null);
        initListener();
        initData();
        return view;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        array_type_ll = view.findViewById(R.id.array_type_ll);
        array_type_iv = view.findViewById(R.id.array_type_iv);

        recycler_view = view.findViewById(R.id.recycler_view);
        decor = new GridSpacingItemDecoration(2, 6, false);
        shopPointsAdapter = new ShopPointsAdapter(getHoldingActivity());
        recycler_view.setAdapter(shopPointsAdapter);
        if (arrayType == 0) {
            recycler_view.setLayoutManager(new GridLayoutManager(getHoldingActivity(), 2));
            recycler_view.addItemDecoration(decor);
        } else {
            recycler_view.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
            recycler_view.removeItemDecoration(decor);
        }
    }


    @Override
    protected void initListener() {
        array_type_ll.setOnClickListener(this);
        shopPointsAdapter.setOnItemClickListener(new ShopPointsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, GoodsSortBean data, int postion) {
                SkipUtils.toGoodsDetails(getHoldingActivity(), GoodsType.common);
            }
        });
    }

    @Override
    protected void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mDatas.add(new GoodsSortBean("红酒" + i));
        }
        shopPointsAdapter.setArrayType(arrayType);
        shopPointsAdapter.addAllData(mDatas);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.array_type_ll:
                if (arrayType == GRID) {
                    arrayType = LINEAR;
                    array_type_iv.setImageResource(R.mipmap.ic_array_grid);
                    shopPointsAdapter.setArrayType(LINEAR);
                    recycler_view.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
                    recycler_view.removeItemDecoration(decor);
                    shopPointsAdapter.notifyDataSetChanged();
                } else {
                    arrayType = GRID;
                    array_type_iv.setImageResource(R.mipmap.ic_array_vertical);
                    shopPointsAdapter.setArrayType(GRID);
                    recycler_view.setLayoutManager(new GridLayoutManager(getHoldingActivity(), 2));
                    recycler_view.addItemDecoration(decor);
                    shopPointsAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }
}
